/********************************************************************************
  * @file     SD93F115B_iic.h
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
**********************************************************************************/


/* Define to prevent recursive inclusion */
#ifndef __SD93F115B_IIC_H
#define __SD93F115B_IIC_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Exported types */
#include "SD93F115B.h"  
 
/********************I2C Init structure definition*******************/
typedef struct
{			 
  uint16_t I2C_BaudRatePrescaler;              //设置I2C的通信速率       
  uint32_t I2C_Mode;                           //配置主从工作模式
  uint16_t I2C_AddressMode;                    //配置几位寻址方式

  FunctionalState I2C_Generate_Ack;            //使能应答信号
  
}I2C_InitTypeDef;

/**************************I2C_BAUDRATE*******************************/
#define IS_I2C_BaudRatePrescaler(Prescaler) (((Prescaler) >=  0) &&((Prescaler) <= 0xFFFF))

/**************************I2C_BAUDRATE*******************************/
#define IS_I2C_Timeout(Time) (((Time) >1) &&((Time) <= 0xFFFF))

/*****************************I2C_mode********************************/
#define I2C_Mode_Master                  I2C_CR_MST
#define I2C_Mode_Salve                   ((uint32_t)0x00000000)

#define IS_I2C_Mode(Mode) (((Mode) == I2C_Mode_Master) || ((Mode) == I2C_Mode_Salve))

/**********************I2C_acknowledged_address*********************/
#define I2C_AddressMode_7bit    		  ((uint16_t)0x0000)
#define I2C_AddressMode_10bit   		  I2C_OAR_ADDMODE

#define IS_I2C_AddressMode(Mode) (((Mode) == I2C_AddressMode_7bit)||((Mode) == I2C_AddressMode_10bit))	

/******************I2C_own_address1**********************************************/
#define IS_I2C_Own_Address7bit(Address)   ((Address) <= (uint16_t)0x007F)
#define IS_I2C_Own_Address10bit(Address)  ((Address) <= (uint16_t)0x03FF)

/**********************I2C_acknowledged_address***********************************/
#define I2C_Direction_Write               ((uint8_t)0x00)
#define I2C_Direction_Read                ((uint8_t)0x01)

#define IS_I2C_Direction(Direction) (((Direction) == I2C_Direction_Read)||((Direction) == I2C_Direction_Write))

/**********************I2C_interrupts_definition ***********************/
#define  I2C_IT_STTIE                    I2C_CR_STTIE 
#define  I2C_IT_ADDRIE                   I2C_CR_ADDRIE 
#define  I2C_IT_BTFIE                    I2C_CR_BTFIE 
#define  I2C_IT_ADDR10IE                 I2C_CR_ADDR10IE
#define  I2C_IT_STOPIE                   I2C_CR_STOPIE
#define  I2C_IT_RXIE                     I2C_CR_RXIE 
#define  I2C_IT_TXIE                     I2C_CR_TXIE  
#define  I2C_IT_BERRIE                   I2C_CR_BERRIE 
#define  I2C_IT_ARLOIE                   I2C_CR_ARLOIE
#define  I2C_IT_NACKIE                   I2C_CR_NACKIE
#define  I2C_IT_OVRIE                    I2C_CR_OVRIE 
#define  I2C_IT_TIMEOUTIE                I2C_CR_TMEOUTIE

#define IS_I2C_ITSource(IT) ((((IT) & (uint32_t)0xF000FFFF) == 0x00) && ((IT)!= 0x00))

/*********************I2C_flags_definition************************/
#define  I2C_Flag_STT                   I2C_SR_STT
#define  I2C_Flag_ADDR                  I2C_SR_ADDR
#define  I2C_Flag_BTF                   I2C_SR_BTF
#define  I2C_Flag_ADDR10                I2C_SR_ADDR10
#define  I2C_Flag_STOPF                 I2C_SR_STOPF
#define  I2C_Flag_RXNE                  I2C_SR_RXNE
#define  I2C_Flag_TXE                   I2C_SR_TXE
#define  I2C_Flag_BERR                  I2C_SR_BERR
#define  I2C_Flag_ARLO                  I2C_SR_ARLO
#define  I2C_Flag_NACKF                 I2C_SR_NACKF
#define  I2C_Flag_OVR                   I2C_SR_OVR
#define  I2C_Flag_TIMEOUT               I2C_SR_TIMEOUT
#define  I2C_Flag_TRA                   I2C_SR_TRA
#define  I2C_Flag_GENCALL               I2C_SR_GENCALL
#define  I2C_Flag_BUSY                  I2C_SR_BUSY
#define  I2C_Flag_SCLLOW                I2C_SR_SCLLOW

#define IS_I2C_ClearFlag(Flag)  (((Flag) == I2C_Flag_ADDR)||((Flag) == I2C_Flag_STOPF)||\
								 ((Flag) == I2C_Flag_BERR)||((Flag) == I2C_Flag_ARLO))||\
								 ((Flag) == I2C_Flag_NACKF)||((Flag) == I2C_Flag_OVR) ||\
								 ((Flag) == I2C_Flag_TIMEOUT)
								 
								 
#define IS_I2C_StatusFlag(Flag) (((Flag) == I2C_Flag_STT)||((Flag) == I2C_Flag_ADDR)  ||\
								 ((Flag) == I2C_Flag_BTF)||((Flag) == I2C_Flag_ADD10) ||\
								 ((Flag) == I2C_Flag_STOPF)||((Flag) == I2C_Flag_RXNE) ||\
								 ((Flag) == I2C_Flag_TXE)||((Flag) == I2C_Flag_BERR)  ||\
								 ((Flag) == I2C_Flag_ARLO)||((Flag) == I2C_Flag_NACKF)||\
								 ((Flag) == I2C_Flag_OVR) || ((Flag) == I2C_Flag_TIMEOUT) ||\
								 ((Flag) == I2C_Flag_TRA) || ((Flag) == I2C_Flag_GENCALL) ||\
								 ((Flag) == I2C_Flag_BUSY)|| ((Flag) == I2C_Flag_SCLLOW))

/* Initialization and Configuration functions *********************************/
void I2C_DeInit(void);                                                              //将I2C相关寄存器恢复为上电默认值
void I2C_Init(I2C_InitTypeDef* I2C_InitStruct);                                     //初始化I2C相关配置
void I2C_Cmd(FunctionalState NewState);                                             //使能I2C通信功能

void I2C_ModeConfig(uint32_t I2C_Mode);                                             //配置I2C工作模式
void I2C_GenerateACKCmd(FunctionalState NewState);                                  //使能或失能I2C产生应答信号
void I2C_GenerateStartCmd(FunctionalState NewState);                                //使能或失能I2C主机模式下产生起始信号
void I2C_GenerateStopCmd(FunctionalState NewState);                                 //使能或失能I2C主机模式下产生停止信号

void I2C_NOStretchClockCmd(FunctionalState NewState);                               //使能或失能I2C从模式下禁止时钟延长
void I2C_ClockSyncCmd(FunctionalState NewState);                                    //使能或失能主机接收模式下,时钟同步功能

void I2C_MRStopModeCmd(FunctionalState NewState);							        //使能或失能主机接收模式下的关闭通信方式时，读取1次数据，默认（DISBALE)读取两次

void I2C_GeneralCallCmd(FunctionalState NewState);							        //使能或失能I2C广播呼叫地址功能

void I2C_SoftwareResetCmd(FunctionalState NewState);						        //使能I2C软件复位功能，主要复位I2C内部电路模块，释放I2C总线，并将状态寄存器复位成上电默认值 

void I2C_ClockTimeoutConfig(uint16_t Timeout,FunctionalState NewState);             //配置和使能或失能I2C超时检测功能

/***************Set I2C Own Address**********/
void I2C_OwnAddressConfig(uint16_t I2C_AddressMode, uint16_t Address);              //配置本机地址
void I2C_SendAddress(uint16_t Address,uint8_t I2C_Direction);                       //主机发送地址,包括10位地址接收模式时，已自动产生重复起始条件

/* Data transfers management functions ****************************************/
void I2C_SendData(uint8_t Data);                                                    //发送数据
uint8_t I2C_ReceiveData(void);                                                      //接收数据

/* Interrupts and flags management functions **********************************/
void I2C_ITConfig(uint32_t I2C_IT, FunctionalState NewState);                       //使能或失能I2C的相关中断
FlagStatus I2C_GetFlagStatus(uint32_t I2C_Flag);                                    //获取I2C状态标志位
void I2C_ClearFlag(uint32_t I2C_Flag);                                              //清除I2C对应中断的标志位

#ifdef __cplusplus
}
#endif

#endif /* __SD93F115B_IIC_H */


/**********************************END OF FILE**********************************/








